
package at.fhj.itm09.snake.views;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;


/**
 * @author Susanne Brandstätter - Susanne.Brandstaetter.ITM09@fh-joanneum.at
 * @author Katharina Biela - Katharina.Biela.ITM09@fh-joanneum.at
 *
 * This is a view variant and handles arrays of "icons" or other drawables.
 */
public class TileView extends View {

	/**
	 * The default size for the drawables in pixels.
	 */
    private static final int tileSize = 14;

    /**
     * 2 variables to save the count of the possible tiles
     */
    protected static int xTileCountInScreen;
    protected static int yTileCountInScreen;

    /**
     * Some variables to handle the gap
     */
    private static int xGap;
    private static int yGap;


    /**
     * A hash where the drawables (wall, green, red, yellow and 0) will be saved.
     */
    private Bitmap[] tileArray; 

    /**
     * At this two-dimensional array the drawables will be saved by their coordinates.
     * e.g.	GREEN x=10 y=15
     * 		tileGrid[10][15] = GREEN;
     */
    private int[][] tileGrid;

    private final Paint paint = new Paint();

    public TileView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public TileView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }



    /**
     * Resets the internal array of the drawables, and
     * sets the maximum index of tiles to be inserted
     * 
     * @param tilecount
     */

    public void resetTiles(int tilecount) {
        tileArray = new Bitmap[tilecount];
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	/*
    	 * We have to calculate the count of the possible drawables
    	 */
        xTileCountInScreen = (int) Math.floor(w / tileSize);
        yTileCountInScreen = (int) Math.floor(h / tileSize);

        /*
         * If the division above gets no even number, than we have to handle
         * with gaps. 
         */
        xGap = ((w - (tileSize * xTileCountInScreen)) / 2);
        yGap = ((h - (tileSize * yTileCountInScreen)) / 2);

        /*
         * Then we have to set the maximum index for the grid array. 
         */
        tileGrid = new int[xTileCountInScreen][yTileCountInScreen];
        clearTiles();
    }

    /**
     * Loads the drawable and converts it into a bitmap. 
     * It also puts the bitmap into the array at the specific index. 
     * 
     * @param key
     * @param tile
     */
    public void loadTile(int key, Drawable tile) {
        Bitmap bitmap = Bitmap.createBitmap(tileSize, tileSize, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        tile.setBounds(0, 0, tileSize, tileSize);
        tile.draw(canvas);

        tileArray[key] = bitmap;
    }

    /**
     * Clears the whole screen from drawables. 
     */
    public void clearTiles() {
        for (int x = 0; x < xTileCountInScreen; x++) {
            for (int y = 0; y < yTileCountInScreen; y++) {
                setTile(0, x, y);
            }
        }
    }

    /**
     * Puts the particular drawable with its coordinates into the grid array. 
     * At this coordinates the drawable would be drawn afterwards. 
     * 
     * @param tileindex
     * @param x
     * @param y
     */
    public void setTile(int tileindex, int x, int y) {
        tileGrid[x][y] = tileindex;
    }


    /**
     * Draws all drawables on the screen. 
     */
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int x = 0; x < xTileCountInScreen; x += 1) {
            for (int y = 0; y < yTileCountInScreen; y += 1) {
                if (tileGrid[x][y] > 0) {
                    canvas.drawBitmap(tileArray[tileGrid[x][y]], 
                    			xGap + x * tileSize,
                                yGap + y * tileSize,
                                paint);
                }
            }
        }
    }
}